/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-14 17:38:59
 * @Description  : 试卷接口的验证中间件
 */
const Joi = require('joi')
const { Op } = require('sequelize')
const R = require('@/models/Response')
const { Paper } = require('@/models/model')
const { ERROR_USER_CODE, ERROR_SYSTEM_CODE } = require('@/constants/errorCode')
const { IS_INTEGER, IS_UNSAFE_INTEGER } = require('@/constants/joiSchema')
const { dumpCheck: globalDumpCheck } = require('@/utils/dumpCheck')

const paperSchema = {
  paperId: IS_UNSAFE_INTEGER,
  paperIds: Joi.array().items(IS_UNSAFE_INTEGER),
  chapterId: IS_UNSAFE_INTEGER,
  page: IS_INTEGER.min(1).default(1),
  pageSize: IS_INTEGER.min(1).default(999),
  name: Joi.string().min(1).max(50),
  pic: Joi.string().allow(''),
  sort: IS_UNSAFE_INTEGER,
  people: IS_UNSAFE_INTEGER.allow(''),
  time: IS_UNSAFE_INTEGER.allow(''),
  url: Joi.string().allow('')
}

/**
 * @description 试卷列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listPaperValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: paperSchema.page,
    pageSize: paperSchema.pageSize,
    name: paperSchema.name,
    chapterId: paperSchema.chapterId
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.newQuery = schema.value
  await next()
}

/**
 * @description 创建试卷时的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.createPaperValidate = async (ctx, next) => {
  const schema = Joi.object({
    name: paperSchema.name.required(),
    chapterId: paperSchema.chapterId.required(),
    pic: paperSchema.pic,
    people: paperSchema.people,
    time: paperSchema.time,
    url: paperSchema.url
  }).validate(ctx.request.body)

  if (schema.error) {
    console.log(schema.error, 'error')
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { chapterId } = ctx.request.body

  const b4 = await globalDumpCheck.checkChapterIds([chapterId])
  // TODO 验证pid的合法性
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.CHAPTER_ID_ERROR)
    return
  }

  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 更新试卷时的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.updatePaperValidate = async (ctx, next) => {
  const schema = Joi.object({
    paperId: paperSchema.paperId.required(),
    name: paperSchema.name.required(),
    pic: paperSchema.pic,
    people: paperSchema.people,
    time: paperSchema.time,
    chapterId: paperSchema.chapterId,
    url: paperSchema.url
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    console.log(schema.error, 'error')

    return
  }

  //TODO 验证 pid 和 paperId的合法性
  const { paperId, chapterId } = ctx.request.body
  const b4 = await globalDumpCheck.checkPaperIds([paperId])
  // const b5 = await globalDumpCheck.checkPaperIds([pid], true)
  const b5 = await globalDumpCheck.checkChapterIds([chapterId])
  // TODO 验证pid的合法性
  if (!b5) {
    ctx.body = R.error(ERROR_USER_CODE.CHAPTER_ID_ERROR)
    return
  }
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.PAPER_ID_ERROR)
    return
  }

  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量删除试卷接口的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.delPapersValidate = async (ctx, next) => {
  const schema = Joi.object({
    paperIds: paperSchema.paperIds.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { paperIds } = ctx.request.body

  // 验证试卷ID数组的合法性
  const b4 = await globalDumpCheck.checkPaperIds(paperIds)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.PAPER_ID_ERROR)
    return
  }
  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量变更章节的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.changeChapterValidate = async (ctx, next) => {
  const schema = Joi.object({
    paperIds: paperSchema.paperIds.required(),
    chapterId: paperSchema.chapterId.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    console.log(ctx.request.body, '=======')

    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { paperIds, chapterId } = ctx.request.body

  // 验证试卷ID数组的合法性
  const b4 = await globalDumpCheck.checkPaperIds(paperIds)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.PAPER_ID_ERROR)
    return
  }

  const b5 = await globalDumpCheck.checkChapterIds([chapterId])
  // TODO 验证pid的合法性
  if (!b5) {
    ctx.body = R.error(ERROR_USER_CODE.CHAPTER_ID_ERROR)
    return
  }
  await next()
}

module.exports.getPaperByIdValidate = async (ctx, next) => {
  const schema = Joi.object({
    paperId: paperSchema.paperId.required()
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { paperId } = ctx.query
  const b4 = await globalDumpCheck.checkPaperIds([paperId])
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.PAPER_ID_ERROR)
    return
  }
  await next()
}
